<?php

namespace app\validate\admin;

use think\Validate;

class ManageValidate extends Validate
{
	protected $rule = [
        'name'   => 'require|length:2,20|regex:/^[\x{4e00}-\x{9fa5}a-zA-Z0-9_]+$/u',
        'email'  => 'require|email',
        'role' => 'require|integer',
        'status' => 'require',
        '_token' => 'require|token:_token',
    ];

    protected $message = [
        'name.require'     => '姓名必须填写',
        'name.length'      => '名字过短或过长3~20',
        'name.regex'        => '名字格式不正确',
        'email.require'    => '邮箱必须填写',
        'email.email'       => '邮箱格式不正确',
        'password.require' => '密码必须填写',
        'password.length'  => '密码过短或过长6~20',
        'password.regex'    => '密码格式不正确',
        'old_password.require' => '原密码必须填写',
        'old_password.length' => '原密码过短或过长6~20',
        'old_password.regex'    => '原密码格式不正确',
        'password_confirmation.require' => '第二次密码必须填写',
        'password_confirmation.length' => '第二次密码过短或过长6~20',
        'password_confirmation.regex'    => '第二次密码格式不正确',
        'role'   => '角色必须选择',
        'status.require'   => '状态必须选择',
        '_token.require' => 'toke令牌为空',
    ];

    /**
     * 新增场景加入password规则
     * @return $this
     */
    public function sceneCreate()
    {
        return $this->append(['password' => 'require|length:6,20|regex:/^[a-zA-Z0-9_]+$/']);
    }

    /**
     * 修改密码场景规则
     * @return $this
     */
    public function scenePassword()
    {
        return $this->append([
            'password' => 'require|length:6,20|regex:/^[a-zA-Z0-9_]+$/',
            'old_password' => 'require|length:6,20|regex:/^[a-zA-Z0-9_]+$/',
            'password_confirmation' => 'require|length:6,20|regex:/^[a-zA-Z0-9_]+$/',
        ])->only(['_token', 'password', 'old_password', 'password_confirmation']);
    }
}
